package com.prezs.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.jdbc.Null;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import com.prezs.enums.Gender;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MyEnumTypeHandler extends BaseTypeHandler<Gender>{

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, Gender parameter, JdbcType jdbcType) throws SQLException {
		ps.setString(i, parameter.getGenderCode());
		log.info("{}",parameter);
	}

	@Override
	public Gender getNullableResult(ResultSet rs, String columnName) throws SQLException {
		log.info("{}", rs,columnName);
		// 获取当前行指定列的值
		Object object = rs.getObject(columnName);
		// 将jdbc类型转换成java类型
		String genderCode = object != null && object instanceof String ? (String) object : null;
		// 返回枚举值
		return Gender.getEnumValue(genderCode);
	}

	@Override
	public Gender getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		log.info("{}", columnIndex);
		Object object = rs.getObject(columnIndex);
		
		String genderCode = object != null && object instanceof String ? (String)object :null;
		
		return Gender.getEnumValue(genderCode);
	}

	@Override
	public Gender getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		log.info("{}", columnIndex);
		Object object = cs.getObject(columnIndex);
		
		String genderCode = object != null && object instanceof String ? (String)object :null;
		
		return Gender.getEnumValue(genderCode);
	}


}
